use std::process::{Command, Stdio};
use std::io::prelude::*;
static PANGRAM: &'static str = "The quick brown fox jumps over the lazy dog";
fn main() {
    // 子进程管道应用示例`wc`命令使用
    let process = match Command::new("wc")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn(){
        Err(why) => panic!("couldn't spawn wc: {:?}", why),
        Ok(process) => process,
    };
//     将字符串写入`wc`子进程的标准输入
// `stdin`拥有`Option<ChildStdin>`类型,且已经知道该实例不为空,直接使用unwrap()方法
    match process.stdin.unwrap().write_all(PANGRAM.as_bytes()) {
        Err(why) => panic!("couldn't write to wc stdin: {}", why),
        Ok(_) => println!("successfully wrote to wc"),
    }
//     `stdin`的生命周期已经结束,`wc`子进程的标准输入已经关闭
//     这点非常重要，否则wc就不会开始处理我们刚刚发送的输入
//     `stdout`拥有`Option<ChildStdout>`类型,且已经知道该实例不为空,直接使用unwrap()方法
    let mut s = String::new();
    match process.stdout.unwrap().read_to_string(&mut s) {
        Err(why) => panic!("couldn't read wc stdout: {}", why),
        Ok(_) => println!("{}", s),
    }
}


